package com.docshare.upload;

import java.io.UnsupportedEncodingException;

import org.red5.io.amf.AMF;
import org.red5.io.amf3.ByteArray;

public class Util {
    public static void writeLong(ByteArray barr, long num) {
        String str = num + "";
        writeString(barr, str);
    }

    public static long readLong(ByteArray barr) {
        String str = readString(barr);
        return Long.parseLong(str);
    }

    public static void writeString(ByteArray barr, String str) {
        // @temp tonyy
        // if("".equals(str)) {}

        barr.writeInt(str.length());

        //java.nio.ByteBuffer buf = AMF.CHARSET.encode(string);
        //encoded = new byte[buf.limit()];
        //buf.get(encoded);
        barr.writeUTFBytes(str);
    }

    public static String readString(ByteArray barr) {
        int length = barr.readInt();
        byte[] bytes = new byte[length];
        barr.readBytes(bytes);
        String str = null;

        try {
            // @temp tonyy
            // final String string = AMF.CHARSET.decode(strBuf).toString();
            //buf.limit(limit); // Reset the limit
            str = new String(bytes, "UTF-8");
        }
        catch(UnsupportedEncodingException e) {
            // should never get here
            e.printStackTrace();
        }

        return str;
    }
}
